import logging

from qiniu import Auth, put_data

# 需要填写你的 Access Key 和 Secret Key
access_key = 'zTzYTy2Qaiwcufv2W2tEMWY8z_X8KyMQE55O2Qog'
# zTzYTy2Qaiwcufv2W2tEMWY8z_X8KyMQE55O2Qog
secret_key = 'oG9i3fiNqbR1Gy4yTUxV2lKkh24jhQck94NYQJKW'
# oG9i3fiNqbR1Gy4yTUxV2lKkh24jhQck94NYQJKW

# 要上传的空间
bucket_name = 'vadie'


def storage(data):
    """七牛云存储上传文件接口"""
    if not data:
        return None
    try:
        # 构建鉴权对象
        q = Auth(access_key, secret_key)
        print("q:", q)
        print("-----------1----------")
        # 生成上传 Token，可以指定过期时间等
        token = q.upload_token(bucket_name)
        print("token=", token)
        # 上传文件
        print("---------2-------------")
        ret, info = put_data(token, None, data)
        print(ret)
        print(info)

    except Exception as e:
        logging.error(e)
        raise e

    if info and info.status_code != 200:
        raise Exception("上传文件到七牛失败")

    # 返回七牛中保存的图片名，这个图片名也是访问七牛获取图片的路径
    return ret["key"]


# 腾讯云上传文件(上传的是文件 而我们传过来的是对象)
# from qcloud_cos import CosConfig, CosS3Client
#
# secret_id = "AKIDgCWaaO6DEzThPYhA0atkhOoENUNzY5Wl"
#
# # secret_key
# secret_key = "A3Ejzyg9NpNehX5jAf5LbWKLN9i7Lalg"
#
# # COS Region
# region = "ap-nanjing"
#
# # COS bucketname
# bucket_name = "mishop-1258846019"
# token = None  # 使用临时密钥需要传入Token，默认为空,可不填
# config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token)  # 获取配置对象
# client = CosS3Client(config)


# def storage(data):
#     # 判断data是否为空
#     if not data:
#         return None
#
#     with open('head01.jpg', 'rb') as fp:
#         response = client.put_object(
#             Bucket='mishop-1258846019',  # Bucket由bucketname-appid组成
#             Body=fp,
#             Key=file_name,
#             StorageClass='STANDARD',
#             ContentType='image/jpeg; charset=utf-8'
#         )
#         print(response['ETag'])


# 文件流 简单上传
# file_name = 'test.txt'
# with open('test.txt', 'rb') as fp:
#     response = client.put_object(
#         Bucket='mishop-1258846019',  # Bucket由bucketname-appid组成
#         Body=fp,
#         Key=file_name,
#         StorageClass='STANDARD',
#         ContentType='text/html; charset=utf-8'
#     )
#     print(response['ETag'])

# with open('head01.jpg', 'rb') as fp:
#     print("fp==",fp)
